from onepassword_tools.lib.OnePasswordUtils import OnePasswordUtils
from onepassword_local_search.OnePassword import OnePassword
from onepassword_tools.lib.MiscUtils import generate_password
from onepassword_tools.lib.OnePasswordServerItem import OnePasswordServerItem
from onepassword_tools.lib.ClickUtils import ClickUtils
import click
import copy
import sys


@click.command()
@click.option('--host', help='Host where the account is created', prompt=True, required=True)
@click.option('--username', help='Account username', prompt=True, required=True)
@click.option('--password', help='Password to use, default autogenerated')
@click.option('--password-length', help='Autogenerated password length, default 25', default=25, type=int)
@click.option('--vault', required=False, help='Vault uuid where to store the information')
def new_server_account(host, username, password, password_length, vault):
    """Create a new Server item in 1Password with the given credentials."""
    NewServerAccount(**locals()).run()


class NewServerAccount:

    entryTitleTemplate = 'USER %s ON %s'
    host: str = None
    onePassword: OnePassword
    onePasswordUtils: OnePasswordUtils
    password: str = None
    password_length: int = 25
    title: str = None
    username: str = None
    vault: str = None

    def __init__(self, **kwargs):
        self.onePasswordUtils = OnePasswordUtils()
        self.onePassword = OnePassword()
        self._init(**kwargs)
        if self.password is None:
            self.password = generate_password(self.password_length)
        self.title = self.entryTitleTemplate % (self.username, self.host)

    def _init(self, **kwargs):
        for key, value in kwargs.items():
            setattr(self, key, value)

    def run(self):

        if not self.onePasswordUtils.is_authenticated():
            self.onePasswordUtils.authenticate()

        uuid = self.save_on_1password()
        if uuid:
            ClickUtils.success('Server password for %s was successfully saved in 1Password with uuid: %s'
                               % (self.host, uuid)
                               )
            sys.exit(0)
        else:
            ClickUtils.error('Unable to save entry in 1Password')
            sys.exit(1)

    def save_on_1password(self):
        arguments = vars(copy.copy(self))
        del arguments['onePassword']
        del arguments['onePasswordUtils']
        server_item = OnePasswordServerItem(**arguments)
        request_object = server_item.get_request_object()
        return self.onePasswordUtils.create_item(
            request_object=request_object,
            template=server_item.item_type,
            title=server_item.get_title(),
            vault=self.vault
        )
